package baiwa.rta.action.user;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Result;


import baiwa.rta.action.BaseAction;
import baiwa.rta.model.Role;
import baiwa.rta.service.RoleService;


@Namespaces(value = { @Namespace("/role") })
public class ROL001AAction extends BaseAction {

	private static final Logger logger = Logger.getLogger(ROL001AAction.class);
	
	private RoleService roleService = lookupBean(RoleService.class, "RoleService");
	private Role role = new Role();
	
	@Action(value = "/ROL001A", results = { @Result(name = "success", location = "ROL001A.tiles", type = "tiles") })
	public String execute() throws Exception {
		logger.info("## "+this.getClass().getSimpleName());
		logger.info("ROL001A execute lists >>>> role: "+ToStringBuilder.reflectionToString(role));

		
		setRole(roleService.findById(role==null? null: role.getRoleId()==null? null:role.getRoleId()));
		
		logger.info("ROL001A execute lists >>>> role: "+ToStringBuilder.reflectionToString(role));
		
		return SUCCESS;
	}
	
	@Action(value = "/saveOrUpdateRole", results = { @Result(name = "success", location="ROL001", type = "redirect" ,params = {"msg.msg", "S001"}) })
	public String save() throws Exception {
		logger.info("## "+this.getClass().getSimpleName());
		logger.info("ROL001A saveOrUpdate >>>> role: "+ToStringBuilder.reflectionToString(role));
		
		if(null == role.getRoleId()){
			roleService.save(role);
		}else{
			roleService.update(role);
		}
		
		return SUCCESS;
	}
	
	@Action(value = "/delete", results = { @Result(name = "success", location="ROL001", type = "redirect" ,params = {"msg.msg", "D001"}) })
	public String delete() throws Exception {
		logger.info("## "+this.getClass().getSimpleName());
		logger.info("ROL001A delete >>>> role: "+ToStringBuilder.reflectionToString(role));
		
		if(null != role.getRoleId()){
			roleService.delete(role);
		}else{
			throw new RuntimeException(" Not Role ID !");
		}
		
		return SUCCESS;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

}
